package com.eascs.web.p2p.loginAction;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.eascs.common.util.StringUtil;
import com.eascs.p2p.common.entity.AdminRoleEntity;
import com.eascs.p2p.common.entity.AdminRoleResourceEntity;
import com.eascs.p2p.common.entity.AdminUserEntity;
import com.eascs.p2p.common.service.IAdminRoleService;
import com.eascs.p2p.common.vo.AdminRoleVo;
import com.eascs.p2p.common.vo.ModelResult;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.web.p2p.utils.StringUtils;

@RestController
@RequestMapping("/act/role/rest")
public class RoleRestController extends BaseRestController{
	@Reference
	private IAdminRoleService  iAdminRoleService;
	
	/**
	 * @Title: updateOrSave
	 * @Description:保存角色对应的资源信息
	 * @author lwx
	 * @param form  roleResJson保存的资源
	 * @return
	 * @return String
	 * @throws
	 * @date 2016年7月11日 下午5:05:45
	 */
	@RequestMapping(value = "/updateOrSave", method = RequestMethod.POST)
	public BaseRestResult<String> updateOrSave(AdminRoleVo form,String roleResJson,HttpServletRequest request) {
		try {
			List<AdminRoleResourceEntity> roleResList=JSON.parseArray(roleResJson, AdminRoleResourceEntity.class);
			AdminRoleEntity sysRole = SysRoleConvert(form,request);
			roleResListConvert(roleResList,request);
			if(StringUtils.isNotEmpty(sysRole.getId())){
				iAdminRoleService.updateRole(sysRole);
			}else {
				sysRole = iAdminRoleService.insertRoleReturnRole(sysRole).getModel();
				for (AdminRoleResourceEntity item : roleResList) {
					item.setRoleId(sysRole.getId());
				}
			}
			if(roleResList.size() > 0){
				iAdminRoleService.insertRoleResBatch(roleResList).getModel();
			}
		} catch (Exception e) {
			return BaseRestResult.warn("保存失败!");
		}
		return BaseRestResult.warn("保存成功!").andRefresh();
	}
	
	
	
	private void roleResListConvert(List<AdminRoleResourceEntity> roleResList,HttpServletRequest request){
		HttpSession session =request.getSession();
		AdminUserEntity userName=(AdminUserEntity) session.getAttribute("adminUser");
		for (AdminRoleResourceEntity adminRoleResourceEntity : roleResList) {
			adminRoleResourceEntity.setUpdateTime(new Date());
			adminRoleResourceEntity.setCreateTime(new Date());
			adminRoleResourceEntity.setUpdator(userName!=null?userName.getAccount():"0");
			adminRoleResourceEntity.setCreator(userName!=null?userName.getAccount():"0");
		}
		
	}
	
	/**
	 * @Title: save
	 * @Description:保存角色
	 * @author lwx
	 * @param form  form
	 * @return
	 * @return String
	 * @throws
	 * @date 2016年7月11日 下午5:05:45
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public BaseRestResult<String> save(HttpServletRequest request,AdminRoleVo form) {
		AdminRoleEntity role = SysRoleConvert(form,request);
		ModelResult<String> modelResult=iAdminRoleService.insertRole(role);
		String result=modelResult.getModel();
		if(StringUtil.isNotBlank(result)){
			;
			BaseRestResult.warn("添加成功!");
			return BaseRestResult.redirect("/act/role/list");
		}else{
			return BaseRestResult.warn("添加失败!");
		}
	}
	
	
	private AdminRoleEntity SysRoleConvert(AdminRoleVo roleform,HttpServletRequest request){
		AdminRoleEntity role=new AdminRoleEntity();
		role.setId(roleform.getId());
		role.setRoleName(roleform.getRoleName());
		role.setRoleCode(roleform.getRoleCode());
		role.setDescription(roleform.getDescription());
		role.setRoleLevel(roleform.getRoleLevel());
		role.setUpdateTime(new Date());
		role.setCreateTime(new Date());
		HttpSession session =request.getSession();
		AdminUserEntity userName=(AdminUserEntity) session.getAttribute("adminUser");
		role.setUpdator(userName!=null?userName.getAccount():"0");
		role.setCreator(userName!=null?userName.getAccount():"0");
		return role;
	}
	/**
	 * @Title: save
	 * @Description:删除角色
	 * @author lwx
	 * @param form  form
	 * @return
	 * @return String
	 * @throws
	 * @date 2016年7月11日 下午5:05:45
	 */
	@RequestMapping("/delete-{id}")
    public BaseRestResult<String> delete(Model model, @PathVariable String id) {
		
		ModelResult<Boolean> modelResult=iAdminRoleService.deleteRole(id);
		boolean result=modelResult.getModel();
		if(result==true){
			return BaseRestResult.warn("删除成功!").andRefresh();
		}else{
			return BaseRestResult.warn("删除失败!");
		}
	}
	
}
